let isSummoning = false

/** 召唤提示词 */
const msg1 = ""

/** 召唤成功提示词 */
const msg2 = "召唤成功"

export class SummonShigure extends plugin {

  constructor() {
    super({
      name: "召唤",
      dsc: "持续召唤直到他回复",
      event: "message",
      priority: 5000,
      rule: [
        {
          reg: `^#召唤`,
          fnc: "startSummon",
          permission: "master",
        },
        {
          reg: "^#结束召唤$",
          fnc: "endSummon"
        }
      ]
    })
  }

  async startSummon(e) {
    const userId = e.msg.replace(/^#召唤/, "").trim() || e.at || e.message.find(item => item.type === "at")?.qq
    if (isSummoning) return e.reply("正在召唤中，请稍候")
    
    const member = e.group.pickMember(userId)
    if (!member || !member.info) return e.reply('群里没有找到Ta哦')
    
    isSummoning = true
    this.userId = userId
    this.e.user_id = userId
    this.setContext("awaitReply")

    while (isSummoning) {
      e.reply([segment.at(userId), msg1])
      await Bot.sleep(1000)
    }
  }

  async endSummon(e) {
    if (!isSummoning) return e.reply("当前没有进行召唤")
    
    isSummoning = false
    e.reply('已结束召唤')
    this.finish("awaitReply")
  }

  async awaitReply(e) {
    isSummoning = false
    e.reply(msg2, true)
    this.finish("awaitReply")
  }
}
